package tech.aistar.day01.homework;

import java.util.Arrays;

public class Employee {
    private int id;
    private String name;
    private double salary;
    //关联
    private Employee[] employees;

    public Employee() {
    }

    public Employee(int id, String name, double salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public Employee[] getEmployees() {
        return employees;
    }

    public void setEmployees(Employee[] employees) {
        this.employees = employees;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", salary=" + salary +
                '}';
    }

    //三个业务方法
    //1.添加下属
    public void addEmp(Employee employee) {
        if (null == employees) {
            System.out.println("sorry,你没有权限添加!");
            return;
        }
        employees = Arrays.copyOf(employees, employees.length + 1);
        employees[employees.length - 1] = employee;
    }

    //2.输出所有下属的信息
    public void findAll() {
        if (null != employees && employees.length > 0) {
            for (Employee employee : employees) {
                System.out.println(employee);
            }
        } else {
            System.out.println("sorry,请先添加下属");
        }
    }

    //3.根据id进行删除
    public void delById(int id) {
        if (null != employees && employees.length > 0) {
            for (int i=0;i<employees.length;i++) {
                if(employees[i].getId()==id){
                    for (int j = i; j <employees.length-1; j++) {
                        employees[j]=employees[j+1];
                    }
                    employees = Arrays.copyOf(employees, employees.length - 1);
                    return;
                }
                else {
                    System.out.println("查无此人,无法删除");
                    return;
                }
            }
        }
        else System.out.println("无下属");
    }

    public void delByPrice(double salary){
        if (null != employees && employees.length > 0){
            for (int i=0;i<employees.length;i++){
                if(employees[i].getSalary()>salary){
                    for (int j = i; j <employees.length-1; j++) {
                        employees[j]=employees[j+1];
                    }
                    employees = Arrays.copyOf(employees, employees.length - 1);
                }
            }
        }else
            System.out.println("请先添加下属");
    }
}